<?php

$this->breadcrumbs=array(
	'Quotation'=>array('index'),
	'View Quotation',
);
$tempQuoNo='';
foreach($modelHeader as $mdlHeader){
	$tempQuoNo= $mdlHeader->quotation_no;
	$flagGenerate=$mdlHeader->generate;
	$po_ref=$mdlHeader->PO_ref;
}

$tempFullAction=true;

//$flagGenerate=QuotationOrderHeader::model()->findByPk($tempQuoNo)->generate;

$this->menu=array(
	array('label'=>'List Quotation', 'url'=>array('index'))
	);

?>

<h1>View Quotation  
	<?php
	if($flagGenerate==1){
		echo '<span class="label label-important"> Closed </span>';
		$tempFullAction=false;
	}
	?>
</h1>


<h3>Header</h3>

<table class='pull-right'>
	<tr>
		<td>
		<?php 
			if(isset(Yii::app()->session['adminUser']) && $flagGenerate==1 && $po_ref!='' ){
				echo CHtml::beginForm( Yii::app()->createUrl('PurchaseOrderHeader/create&quo_no='.$tempQuoNo));
			 	Yii::app()->session['vendorList'] = 0;
			 	echo CHtml::submitButton('Create PO',array(
			 		'class'=>'pull-right',
				));  
				 echo CHtml::endForm();
			 }
		?>
		</td>
	</tr>
</table>

<br />
<br />
<?php
$myclass = get_class($this);
$myclass= strstr($myclass,'Controller',true);
/*$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'quotation-order-header-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		
	),
)); */

$this->widget('application.extensions.tablesorter.Sorter', array(
    'data'=>$modelHeader,
    'columns'=>array(
		'quotation_no',
		'Customer.customer',
		'attention',
		'project',
		'currency',
		'revisi',
	),
	'filters'=>array(
        'filter-false',
		'filter-false',
        'filter-false',
        'filter-false',
        'filter-false',
		'filter-false',
    ),
	'view'=>'controllerView',
	'isViewQuotation'=>true,
	'fullAction'=>$tempFullAction,
));

?>

<br />
<h3>Detail</h3>
<?php 
	if($flagGenerate==0){
		echo CHtml::beginForm( Yii::app()->createUrl('QuotationOrderDetail/create&quo_no='.$tempQuoNo));
	 	echo CHtml::submitButton('Add Item',array(
	   'class' => 'pull-right'
		));  
	}
?> 
<?php echo CHtml::endForm(); ?> 
<br />
<?php

$this->widget('application.extensions.tablesorter.Sorter', array(
    'data'=>$modelDetail,
    'columns'=>array(
		//'quotation_no',
        'product_pn',
        'additional',
        'qty',
        'uom',
        'products.unit_sell',
    ),
	'filters'=>array(
        //'filter-false',
		'filter-select',
        'filter-false',
        'filter-false',
        'filter-false',
        'filter-select',
		'filter-false',
    ),
	'view'=>'QuotationOrderDetail',
	'model'=>3,
	'fullAction'=>$tempFullAction,
));

?>
	<table  class='pull-right'>
		<tr>
			<td>
		 <?php 
		 if($flagGenerate==0){
		 	echo CHtml::button('Finalize',array('id'=>'finalize','onclick'=>'finalize(); $("#dialogFinalize").dialog("open"); return false;')); 
		 }
		 ?>
		</td>
		<td>
		<?php 
			if($flagGenerate==0){
				echo CHtml::beginForm( Yii::app()->createUrl('quotationOrderHeader/revision&quo_no='.$tempQuoNo));
			 	echo CHtml::submitButton('Revision',array(
			 		'class'=>'pull-right',
				));  
				echo CHtml::endForm();
			}
		?>
		</td>
		<td>
			<?php if($flagGenerate==0){ ?>
			<?php echo CHtml::beginForm( Yii::app()->createUrl('quotationOrderHeader/viewPDF&id='.$tempQuoNo)); ?>
			<input type='submit' class='pull-right' onclick="this.form.target='_blank';return true;" value='Generate'> 
			<?php echo CHtml::endForm(); ?>
			<?php } ?> 
		</td>
		</tr>
	</table>
	<br>
<?php

	$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
	    'id'=>'dialogFinalize',
	    'options'=>array(
	        'title'=>'Input PO Reference',
	        'autoOpen'=>false,
	        'modal'=>true,
	        'width'=>650,
	        'height'=>520,
	    ),
	));

?>
	<div id='divForForm' class="divForForm"></div>
	 
	<?php $this->endWidget();?>

<script type="text/javascript">
	// here is the magic
	function finalize()
	{
	    <?php echo CHtml::ajax(array(
            //'url'=>array('triger/create', "wf_id"=>$modelWorkFLow->WORKFLOW_ID),
            'url'=>array('QuotationOrderHeader/close'),
            'data'=> "js:$(this).serialize()",
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {
                if (data.status == 'failure')
                {
                    $('#dialogFinalize div.divForForm').html(data.div);
                          // Here is the trick: on submit-> once again this function!
                    $('#dialogFinalize div.divForForm form').submit(finalize);
                }
                else
                {
                    $('#dialogFinalize div.divForForm').html(data.div);
                    setTimeout(\"$('#dialogFinalize').dialog('close') \",3000);
                    location.reload();
                }
 
            } ",
        ));
        ?>
	    return false;  
	}

	</script>